package com.pencilisland.pay.mgt.console.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pencilisland.pay.mgt.console.bean.JstreeNode;
import com.pencilisland.pay.mgt.console.entity.SysMenu;
import com.pencilisland.pay.mgt.console.entity.SysUser;
import com.pencilisland.pay.mgt.console.repository.MenuRepository;
import com.pencilisland.pay.mgt.console.service.AuthService;
import com.pencilisland.pay.mgt.console.service.MenuService;
import com.pencilisland.pay.mgt.console.service.UserService;
import com.pencilisland.pay.mgt.util.G;

@Component
public class MenuServiceImpl implements MenuService {

	Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	MenuRepository menuRepo;
	@Autowired
	UserService userService;
	@Autowired
	AuthService authService;

	@Override
	public JstreeNode menuTreeData(Long rootId) {
		log.info("获取菜单树数据【{}】", rootId);
		SysMenu menu = menuRepo.findOne(rootId);
		if (menu == null) {
			log.info("菜单信息不存在");
			return null;
		}

		JstreeNode root = new JstreeNode();
		root.setId(G.str(menu.getMenuId()));
		root.setText(menu.getMenuName());
		JstreeNode.State state = new JstreeNode.State();
		root.setState(state);
		root.setLiAttr(menu);

		menuTreeDataChilds(root);

		return root;
	}

	void menuTreeDataChilds(JstreeNode root) {
		List<SysMenu> childMenus = menuRepo.findByMenuParentOrderByMenuWeightAsc(G.l(root.getId()));

		if (childMenus != null && !childMenus.isEmpty()) {
			root.getState().setOpened(true);
			root.setChildren(new ArrayList<JstreeNode>());
			for (SysMenu menu : childMenus) {
				JstreeNode node = new JstreeNode();
				node.setId(G.str(menu.getMenuId()));
				node.setText(menu.getMenuName());
				JstreeNode.State state = new JstreeNode.State();
				node.setState(state);
				node.setLiAttr(menu);

				menuTreeDataChilds(node);

				root.getChildren().add(node);
			}
		} else {
			root.getState().setOpened(false);
		}
	}

	@Override
	public SysMenu save(SysMenu menu) {
		log.info("保存菜单数据");
		return menuRepo.save(menu);
	}

	@Override
	public SysMenu loadData(Long id) {
		return menuRepo.findOne(id);
	}

	@Override
	public void delete(Long id) {
		log.info("删除菜单数据[{}]", id);
		menuRepo.delete(id);
	}

	@Override
	public List<SysMenu> menuSession() {
		SysUser user = userService.findSessionUser();

		SysMenu rootMenu = menuRepo.findOne(1L);
		rootMenu.setChildList(new ArrayList<SysMenu>());

		initMenuSession(rootMenu, user);

		return rootMenu.getChildList();
	}

	public void initMenuSession(SysMenu rootMenu, SysUser user) {
		List<SysMenu> childMenus = menuRepo.findByMenuParentOrderByMenuWeightAsc(rootMenu.getMenuId());
		if (childMenus != null && !childMenus.isEmpty()) {
			if (rootMenu.getChildList() == null)
				rootMenu.setChildList(new ArrayList<SysMenu>());
			for (SysMenu menu : childMenus) {

				if (!authService.isAuth(menu, user)) {
					continue;
				}

				rootMenu.getChildList().add(menu);

				initMenuSession(menu, user);
			}

		}
	}

}
